package com.idbk.chargestation.activity.user;

import android.content.Context;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonLogin;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;

/**
 * Created by wuz on 2019/1/23.
 */

public class ActivityUnbindPhone extends BaseActivity implements View.OnClickListener,TextView.OnEditorActionListener{

    private Context mContext;
    private EditText mEdtPassword;
    private ImageView mImagePassword;
    private String mPassword;
    private boolean mIsPasswordShowFlag = false;
    private static final String TAG = ActivityBindPhone.class.getSimpleName();

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_unbindphone);

        mContext = this;

        initView();
    }

    public void initView(){
        initEdiText();
        initImageView();
        initOnClickView();
    }

    public void initEdiText(){
        mEdtPassword = (EditText) findViewById(R.id.unbindphone_password);
    }

    public void initImageView(){
        mImagePassword = (ImageView)findViewById(R.id.unbindphone_password_visible);
    }

    public void initOnClickView(){
        findViewById(R.id.unbindphone_password_visible).setOnClickListener(this);
        findViewById(R.id.toolbar_back).setOnClickListener(this);
        findViewById(R.id.unbind).setOnClickListener(this);
        mEdtPassword.setOnEditorActionListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.toolbar_back :
                finish();
                break;
            case R.id.unbindphone_password_visible ://密码状态切换
                setPasswordVisible();
                break;
            case R.id.unbind ://解绑手机号
                gotounbind();
                break;
            default:
                break;
        }
    }

    public void gotounbind(){
        if (checkInput()){
            unbind();
        }
    }

    private boolean checkInput(){
        mPassword = mEdtPassword.getText().toString();
        if ( mPassword.length() != 0) {
            if ( mPassword.length() < 6 || mPassword.length() > 20 ) {
                Toast.makeText(this, this.getResources().getString(R.string.tip_please_input_password), Toast.LENGTH_SHORT).show();
                mEdtPassword.setFocusable(true);
                return false;
            }
        }
        return true;
    }

    private void unbind(){
        showMyProgressDialog(true, false, "解绑中..." );
        mMainRequest = ChargeStationAPI.unBindPhone(mPassword, mResponse);
    }

    private final EHttpResponse mResponse = new EHttpResponse() {
        @Override
        public void onEResponse(String response) {
            JsonLogin result  = GsonUtils.toBean(JsonLogin.class,response);
            if (handleResponseStatus(result)){
                setResult(RESULT_OK);
                showResult(mContext, "解绑成功!", true);
            }
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            Log.e(TAG, error.getMessage(), error.getCause());
            Toast.makeText(mContext, R.string.common_tip_resultfail, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEFinish() {
            super.onEFinish();
            dismissMyProgressDialog();
        }
    };



    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        return false;
    }

    private void setPasswordVisible() {
        if (mIsPasswordShowFlag) {
            mIsPasswordShowFlag = false;
            mImagePassword.setImageResource(R.drawable.btn_login_password_hidden);
            mEdtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            mIsPasswordShowFlag = true;
            mImagePassword.setImageResource(R.drawable.btn_login_password_visible);
            mEdtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }
}
